package com.chang.product.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chang.common.utils.PageUtils;
import com.chang.common.utils.R;
import com.chang.product.entity.BrandEntity;
import com.chang.product.entity.CategoryBrandRelationEntity;
import com.chang.product.service.CategoryBrandRelationService;
import com.chang.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 品牌分类关联
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    // 获取当前分类下所有的品牌
    @GetMapping("brands/list")
    public R brandList(@RequestParam(value = "catId") Long catId) {
        List<BrandEntity> Brandlist = categoryBrandRelationService.getbrandsByCatId(catId);
        List<BrandVo> brandVos = new ArrayList<>();
        Brandlist.forEach((item) -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandName(item.getName());
            brandVo.setBrandId(item.getBrandId());
            brandVos.add(brandVo);
        });
        System.out.println("brandVos========>>>>>" + brandVos);
        return R.ok().put("data", brandVos);
    }


    // 获取当前品牌关联的所有分类列表
    @GetMapping("catelog/list")
    public R catelogList(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getBrandId, brandId));
        return R.ok().put("data", data);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /*
     * @Author Chang
     * @Date 2024/3/6 9:30
     * @Description 这里前端只会传递过来品牌id和属性分类id，所以需要根据id查name然后再set保存
     * @Param [categoryBrandRelation]
     * @Return com.chang.common.utils.R
     * @Since version 1.0
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        categoryBrandRelationService.saveDeatil(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
